# Import all required libraries
from python_a2a.mcp import FastMCP
import yfinance as yf

yfinance_mcp = FastMCP(
    name="YFinance MCP",
    version="1.0.0",
    description="Stock market data tools"
)

@yfinance_mcp.tool()
def get_stock_price(ticker: str) -> dict:
    """Get current stock price for a given ticker symbol."""
    try:
        # 获取股票数据
        stock = yf.Ticker(ticker)
        data = stock.history(period="1d")
        
        if data.empty:
            return {"error": f"No data found for ticker {ticker}"}
        
        # 提取价格
        price = data['Close'].iloc[-1]
        
        return {
            "ticker": ticker,
            "price": price,
            "currency": "USD",
            "timestamp": data.index[-1].strftime('%Y-%m-%d %H:%M:%S')
        }
    except Exception as e:
        return {"error": f"Error fetching stock data: {str(e)}"}
if __name__ == "__main__":
    yfinance_mcp.run(host="localhost", port=5002)